
Version = Class.create();

Version.prototype = {
  
  NUMBER: 0.8,
  
  DOWNLOAD_HOME: 'http://ccmonitor.googlecode.com/files/',
  
  initialize: function() {
    $('version-span').style.top = '11px'
    $('version-span').innerHTML = this.NUMBER
  },
  
  source: function() {
    // cc-monitor-vX.X-update.json
    return this.DOWNLOAD_HOME + 'cc-monitor-' + this.NUMBER + '-update.json';
  },
  
  update: function() {
    try {
      AjaxRequests.create(this.source(), {
        method: 'get',
        requestHeaders: {"Cache-Control": "no-cache"},
        onSuccess: this.updateVersion.bind(this)
      });
    }catch (e) {
      alert("can't get cc monitor update version info from source(" + this.source() + "): " + e);
    }
  },
  
  updateVersion: function(transport) {
    var remoteVersion = null;
    try {
      remoteVersion = transport.responseText.evalJSON(false);
    } catch (e) {
      alert("Can't parse version info: " + e);
      return;
    }
    if(remoteVersion.number > this.NUMBER) {
      $('version-span').innerHTML = '';
      $('version-span').style.top = '5px'
      $('version-span').appendChild(this.downloadLink(remoteVersion));
    }
  },
  
  downloadLink: function(version) {
    var img = Builder.node('img', {src: 'images/dance.gif', 'class': 'spinner'});
    return Builder.node('a', {
      href: version.updateLink,
      title: 'CC Monitor version ' + version.number + ' is ready, click to download it. ' + (version.description || ''),
      target: '_blank', 
      onclick: 'openLink(this)'
    }, img);
  }
}